package Eclass.Schedule;

import ibatis.IbatisHelper;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;

public class ScheduleManager {
	private SqlMapClient ibatis;

	public ScheduleManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	public List getAllSchedule(String userId, String role, int batchId, int currentPage) {
		List result = null;
		
		int startNo = ((currentPage - 1) * 5) + 1;
		int endNo = currentPage * 5;
		
		System.out.println("number : " + startNo + " - " + endNo + "\n\n");
				
		try {
			Map map = new HashMap();
			if("trainer".equalsIgnoreCase(role)) {
				map.put("trainerUserId", userId);
			} else{
				map.put("userId", userId);
			}
			
			map.put("role", role);
			map.put("batchId", batchId);
			map.put("startNo", startNo);
			map.put("endNo", endNo);
			
			result = ibatis.queryForList("schedule.getAllSchedule", map);
			
			System.out.println("result : " + result.size() + "\n\n");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Integer getTotalPage(String userId, String role, int batchId) {
		Integer result = 0;
				
		try {
			Map map = new HashMap();
			if("trainer".equalsIgnoreCase(role)) {
				map.put("trainerUserId", userId);
			} else{
				map.put("userId", userId);
			}
			
			map.put("role", role);
			map.put("batchId", batchId);
			
			result = (Integer)(ibatis.queryForObject("schedule.getTotalPage", map));
			
			System.out.println("totalPage : " + result + "\n\n");
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List getBatch() {
		List result = null;
				
		try {
			result = ibatis.queryForList("schedule.getBatch", "");
			
			System.out.println("result list batch : " + result.size() + "\n\n");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
